package com.zmn.plat.dubbo.impl.orderconfig.documentarycontent;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.business.interfaces.orderconfig.documentarycontent.DocumentaryContentBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.documentarycontent.DocumentaryContentDIO;
import com.zmn.plat.common.dto.orderconfig.documentarycontent.DocumentaryContentDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.documentarycontent.DocumentaryContentListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.orderconfig.documentarycontent.DocumentaryContentQuery;
import com.zmn.plat.model.vo.orderconfig.documentarycontent.DocumentaryContentSimpleVO;
import com.zmn.plat.model.vo.orderconfig.documentarycontent.DocumentaryContentVO;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述:跟单内容
 *
 * @author linfeng
 * @since 2019/10/24/17:40
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class DocumentaryContentListRemoteServiceImpl implements DocumentaryContentListRemoteService {
    private Logger logger = LoggerFactory.getLogger(DocumentaryContentListRemoteServiceImpl.class);
    private static final String TAG = "订单配置-跟单内容 dubbo 接口";

    @Resource
    private DocumentaryContentBService documentaryContentBService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<DocumentaryContentDRO> getById(Integer id) {
        log.info("getById：id:{}", id);

        DocumentaryContentDRO dro = new DocumentaryContentDRO();
        DocumentaryContentVO documentaryContentVO = documentaryContentBService.findByKey(id);
        if (documentaryContentVO == null || documentaryContentVO.getId() == null) {
            logger.error(String.format("[%s]参数:[%s]对应的跟单内容对象不存在", TAG, id));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        BeanCopyUtils.copyProperties(documentaryContentVO, dro);
        dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
        return ResponseDTO.success(dro);
    }

    @Override
    @ReadOnlyConnection
    @SuppressWarnings("Duplicates")
    public ResponseDTO<List<DocumentaryContentDRO>> listByQuery(DocumentaryContentDIO documentaryContentDIO) {
        log.info("listByQuery：documentaryContentDIO:{}", documentaryContentDIO);
        DocumentaryContentQuery documentaryContentQuery = new DocumentaryContentQuery();
        BeanUtils.copyProperties(documentaryContentDIO, documentaryContentQuery);
        List<DocumentaryContentSimpleVO> documentaryContentVOList = Optional.ofNullable(documentaryContentBService.listByQuery(documentaryContentQuery)).orElse(Collections.emptyList());
        BeanCopier copier = BeanCopier.create(DocumentaryContentSimpleVO.class, DocumentaryContentDRO.class, false);
        List<DocumentaryContentDRO> documentaryContentDROList = documentaryContentVOList.stream().map(documentaryContentVO -> {
            DocumentaryContentDRO dro = new DocumentaryContentDRO();
            copier.copy(documentaryContentVO, dro, null);
            dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(documentaryContentDROList);
    }
}
